package com.zj.leetcode.medium.stack;

/**
 * @program: algorithm
 * @description: 文件的最长绝对路径
 * 388 longest-absolute-file-path
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class LongestAbsoluteFilePath {
    public static void main(String[] args) {
        String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
        Solution solution = new LongestAbsoluteFilePath().new Solution();
        System.out.println(solution.lengthLongestPath(input));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int lengthLongestPath(String input) {
            if (input == null || input.length() == 0) {
                return 0;
            }
            String[] words = input.split("\n");
            int[] pathLens = new int[words.length + 1];
            pathLens[0] = -1;
            int ans = 0;

            for (String word : words) {
                int level = word.lastIndexOf('\t') + 1 + 1;
                int nameLen = word.length() - (level - 1);
                pathLens[level] = pathLens[level - 1] + 1 + nameLen;
                if (word.contains(".")) {
                    ans = Math.max(ans, pathLens[level]);
                }
            }
            return ans;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}